import { defHttp } from '/@/utils/http/axios'

import { OrderSO, OrderVO, OrderBO, OrderLogVO, OrderLogSO } from './model/orderModel'
import { BasicFetchResult, BasicPageParams } from '/@/api/model/baseModel'

enum Api {
  pageOrder = '/order/page',
  createOrder = '/order/create',
  updateOrder = '/order/update',
  cancelOrder = '/order/cancel',
  lockOrder = '/order/lock',
  unLockOrder = '/order/unlock',
  deliverOrder = '/order/deliver',
  unDeliverOrder = '/order/unDeliver',
  listAllCreateOrderId = '/order/getAllCreateOrderId',
  exportCheckedOrder = '/order/exportCheckedOrder',
  orderLog = '/order/logByProduct',
  exportLogByProduct = '/order/exportLogByProduct',
}

export const pageOrder = (data: BasicPageParams<OrderSO>) => {
  return defHttp.post<BasicFetchResult<OrderVO>>(
    { url: Api.pageOrder, data },
    { errorMessageMode: 'message' },
  )
}

export const createOrder = (data: OrderBO) => {
  return defHttp.post<BasicFetchResult<boolean>>(
    { url: Api.createOrder, data },
    { errorMessageMode: 'message' },
  )
}

export const updateOrder = (data: OrderBO) => {
  return defHttp.post<BasicFetchResult<boolean>>(
    { url: Api.updateOrder, data },
    { errorMessageMode: 'message' },
  )
}

export const cancelOrder = (data: string) => {
  return defHttp.delete<BasicFetchResult<boolean>>(
    { url: Api.cancelOrder, params: { orderId: data } },
    { errorMessageMode: 'message' },
  )
}

export const lockOrder = (data: string) => {
  return defHttp.get<BasicFetchResult<boolean>>(
    { url: Api.lockOrder, params: { orderId: data } },
    { errorMessageMode: 'message' },
  )
}

export const unLockOrder = (data: string) => {
  return defHttp.get<BasicFetchResult<boolean>>(
    { url: Api.unLockOrder, params: { orderId: data } },
    { errorMessageMode: 'message' },
  )
}

export const deliverOrder = (data: string) => {
  return defHttp.get<BasicFetchResult<boolean>>(
    { url: Api.deliverOrder, params: { orderId: data } },
    { errorMessageMode: 'message' },
  )
}

export const unDeliverOrder = (data: string) => {
  return defHttp.get<BasicFetchResult<boolean>>(
    { url: Api.unDeliverOrder, params: { orderId: data } },
    { errorMessageMode: 'message' },
  )
}

export const listAllCreateOrderId = () => {
  return defHttp.get<string[]>({ url: Api.listAllCreateOrderId }, { errorMessageMode: 'message' })
}

export const exportCheckedOrder = (data: string[], shouldUpdate: boolean) => {
  return defHttp.post<BlobPart>(
    {
      url: Api.exportCheckedOrder,
      data: { orderIds: data, shouldUpdate },
      responseType: 'blob',
    },
    { errorMessageMode: 'message' },
  )
}

export const orderLog = (data: OrderLogSO) => {
  return defHttp.get<OrderLogVO[]>(
    {
      url: Api.orderLog,
      params: data,
    },
    { errorMessageMode: 'message' },
  )
}

export const exportLogByProduct = (data: OrderLogSO) => {
  return defHttp.get<BlobPart>(
    {
      url: Api.exportLogByProduct,
      params: data,
      responseType: 'blob',
    },
    { errorMessageMode: 'message' },
  )
}
